{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Array Transformation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: transformArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组变换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>首先，给你一个初始数组 <code>arr</code>。然后，每天你都要根据前一天的数组生成一个新的数组。</p>\n",
    "\n",
    "<p>第&nbsp;<code>i</code>&nbsp;天所生成的数组，是由你对第&nbsp;<code>i-1</code>&nbsp;天的数组进行如下操作所得的：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>假如一个元素小于它的左右邻居，那么该元素自增 <code>1</code>。</li>\n",
    "\t<li>假如一个元素大于它的左右邻居，那么该元素自减 <code>1</code>。</li>\n",
    "\t<li>首、尾元素 <strong>永不</strong>&nbsp;改变。</li>\n",
    "</ol>\n",
    "\n",
    "<p>过些时日，你会发现数组将会不再发生变化，请返回最终所得到的数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[6,2,3,4]\n",
    "<strong>输出：</strong>[6,3,3,4]\n",
    "<strong>解释：</strong>\n",
    "第一天，数组从 [6,2,3,4] 变为 [6,3,3,4]。\n",
    "无法再对该数组进行更多操作。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[1,6,3,4,3,5]\n",
    "<strong>输出：</strong>[1,4,4,4,4,5]\n",
    "<strong>解释：</strong>\n",
    "第一天，数组从 [1,6,3,4,3,5] 变为 [1,5,4,3,4,5]。\n",
    "第二天，数组从 [1,5,4,3,4,5] 变为 [1,4,4,4,4,5]。\n",
    "无法再对该数组进行更多操作。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= arr.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= arr[i] &lt;= 100</code></li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [array-transformation](https://leetcode.cn/problems/array-transformation/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [array-transformation](https://leetcode.cn/problems/array-transformation/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[6,2,3,4]\\r', '[1,6,3,4,3,5]\\r']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            tmp = copy.deepcopy(arr)\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i - 1] > tmp[i] < tmp[i + 1]:\n",
    "                    arr[i] += 1\n",
    "                elif tmp[i - 1] < tmp[i] > tmp[i + 1]:\n",
    "                    arr[i] -= 1\n",
    "            if arr == tmp:\n",
    "                return arr "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            changed = False\n",
    "            tmp = arr[:]                            #辅助判断\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i-1] < tmp[i] > tmp[i+1]:    #山峰\n",
    "                    arr[i] -= 1\n",
    "                    changed = True\n",
    "                elif tmp[i-1] > tmp[i] < tmp[i+1]:  #山谷\n",
    "                    arr[i] += 1\n",
    "                    changed = True\n",
    "            if changed == False:\n",
    "                break\n",
    "        return arr\n",
    "\n",
    "            \n",
    "\n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        change = True\n",
    "        while change:\n",
    "            change = False\n",
    "        \n",
    "            change_dict = {}\n",
    "            for i in range(1,len(arr)-1):\n",
    "                left = arr[i-1]\n",
    "                mid = arr[i]\n",
    "                right = arr[i+1]\n",
    "\n",
    "                if min(left,right) > mid:\n",
    "                    change_dict[i] =mid+1\n",
    "                    change=True\n",
    "                elif max(left,right) < mid:\n",
    "                    change_dict[i] =mid-1\n",
    "                    change=True\n",
    "            for i in change_dict:\n",
    "                arr[i] = change_dict[i]\n",
    "            # print(arr)\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    "\n",
    "        d = {}\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "                if i in d:\n",
    "                    continue\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "                    d[i] = 0\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    "\n",
    "        d = {}\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "                if i in d:\n",
    "                    continue\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "                    d[i] = 0\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\r\n",
    "        n = len(arr)\r\n",
    "        while True:\r\n",
    "            changed = False\r\n",
    "            tmp = arr[:]                            #辅助判断\r\n",
    "            for i in range(1, n - 1):\r\n",
    "                if tmp[i-1] < tmp[i] > tmp[i+1]:    #山峰\r\n",
    "                    arr[i] -= 1\r\n",
    "                    changed = True\r\n",
    "                elif tmp[i-1] > tmp[i] < tmp[i+1]:  #山谷\r\n",
    "                    arr[i] += 1\r\n",
    "                    changed = True\r\n",
    "            if changed == False:\r\n",
    "                break\r\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            tmp = arr.copy()\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i - 1] > tmp[i] < tmp[i + 1]:\n",
    "                    arr[i] += 1\n",
    "                elif tmp[i - 1] < tmp[i] > tmp[i + 1]:\n",
    "                    arr[i] -= 1\n",
    "            if arr == tmp:\n",
    "                return arr "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            changed = False\n",
    "            tmp = arr[:]                            #辅助判断\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i-1] < tmp[i] > tmp[i+1]:    #山峰\n",
    "                    arr[i] -= 1\n",
    "                    changed = True\n",
    "                elif tmp[i-1] > tmp[i] < tmp[i+1]:  #山谷\n",
    "                    arr[i] += 1\n",
    "                    changed = True\n",
    "            if changed == False:\n",
    "                break\n",
    "        return arr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            tmp = arr.copy()\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i - 1] > tmp[i] < tmp[i + 1]:\n",
    "                    arr[i] += 1\n",
    "                elif tmp[i - 1] < tmp[i] > tmp[i + 1]:\n",
    "                    arr[i] -= 1\n",
    "            if arr == tmp:\n",
    "                return arr "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        if n<3:\n",
    "            return arr\n",
    "        while True:\n",
    "            ans = arr.copy()\n",
    "            # n = len(arr)\n",
    "            cnt = 0\n",
    "            for i in range(1,n-1):\n",
    "                if arr[i-1]<arr[i] and arr[i]>arr[i+1]:\n",
    "                    ans[i] -= 1\n",
    "                    cnt += 1\n",
    "                elif arr[i] < arr[i-1] and arr[i]<arr[i+1]:\n",
    "                    ans[i] += 1\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                return ans\n",
    "            arr = ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n=len(arr)\n",
    "        while True:\n",
    "            change=False\n",
    "            cur=arr[:] ###深拷贝后，cur不受arr列表影响的写法\n",
    "            for i in range(1,n-1):\n",
    "                if arr[i]<arr[i-1] and arr[i]<arr[i+1]:\n",
    "                    # arr[i]+=1\n",
    "                    cur[i]+=1\n",
    "                    change=True\n",
    "                elif arr[i]>arr[i-1] and arr[i]>arr[i+1]:\n",
    "                    # arr[i]-=1\n",
    "                    cur[i]-=1\n",
    "                    change=True \n",
    "            arr=cur \n",
    "            if not change:\n",
    "                break   \n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        t = 0\n",
    "        arr2=[]\n",
    "        while t == 0:\n",
    "            t = 1\n",
    "            for i in range(0,len(arr)):\n",
    "                if i == 0:\n",
    "                    arr2.append(arr[i])\n",
    "                if i > 0 and i < len(arr)-1:\n",
    "                    if arr[i] > arr[i-1] and arr[i] > arr[i+1]:\n",
    "                        arr2.append(arr[i]-1)\n",
    "                        t = 0\n",
    "                    elif arr[i] < arr[i-1] and arr[i] < arr[i+1]:\n",
    "                        arr2.append(arr[i]+1)\n",
    "                        t = 0\n",
    "                    else:\n",
    "                        arr2.append(arr[i])\n",
    "                if i == len(arr)-1:\n",
    "                    arr2.append(arr[i])\n",
    "            if arr == arr2:\n",
    "                t = 1\n",
    "            else:\n",
    "                arr = arr2[:]\n",
    "                arr2 = []\n",
    "            \n",
    "        return arr2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    "\n",
    "        d = {}\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "                if i in d:\n",
    "                    continue\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "                    d[i] = 0\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    "\n",
    "        d = {}\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "                if i in d:\n",
    "                    continue\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "                    d[i] = 0\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n=len(arr)\n",
    "        res=arr[:]\n",
    "        flag=True\n",
    "        while flag:\n",
    "            flag = False\n",
    "            for i in range(1,n-1):\n",
    "                if arr[i] < arr[i-1] and arr[i] < arr[i+1]:\n",
    "                    res[i] = arr[i]+1\n",
    "                    flag=True\n",
    "                elif arr[i] > arr[i-1] and arr[i] > arr[i+1]:\n",
    "                    res[i] = arr[i]-1\n",
    "                    flag = True\n",
    "            arr=res[:]\n",
    "        return res\n",
    "                    \n",
    "\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        flag = True\n",
    "        while flag:\n",
    "            sub_flag = False\n",
    "            copy = arr[:]\n",
    "            for i in range(1, len(arr) - 1, 1):\n",
    "                if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:\n",
    "                    copy[i] -= 1\n",
    "                    sub_flag = True\n",
    "                elif arr[i] < arr[i-1] and arr[i] < arr[i + 1]:\n",
    "                    copy[i] += 1\n",
    "                    sub_flag = True\n",
    "\n",
    "            if not sub_flag:\n",
    "                flag = False\n",
    "            arr = copy\n",
    "        return arr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            tmp = copy.deepcopy(arr)\n",
    "            for i in range(1, n - 1):\n",
    "                if tmp[i - 1] > tmp[i] < tmp[i + 1]:\n",
    "                    arr[i] += 1\n",
    "                elif tmp[i - 1] < tmp[i] > tmp[i + 1]:\n",
    "                    arr[i] -= 1\n",
    "            if arr == tmp:\n",
    "                return arr "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        cpy = arr[:]\n",
    "        for i in range(1, len(arr) - 1):\n",
    "            if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:\n",
    "                cpy[i] += 1\n",
    "            elif arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:\n",
    "                cpy[i] -= 1\n",
    "        return cpy if cpy == arr else self.transformArray(cpy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = res.copy()\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        # 不知道循环次数\n",
    "        while True:\n",
    "            cur=arr[:]\n",
    "            for i in range(1,n-1):\n",
    "                if arr[i]<arr[i-1] and arr[i]<arr[i+1]:\n",
    "                    cur[i]+=1\n",
    "                elif arr[i]>arr[i-1] and arr[i]>arr[i+1]:\n",
    "                    cur[i]-=1\n",
    "            if cur==arr:\n",
    "                print(arr)\n",
    "                return arr\n",
    "            arr=cur[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            cur=arr[:]\n",
    "            for i in range(1,n-1):\n",
    "                if arr[i]<arr[i-1] and arr[i]<arr[i+1]:\n",
    "                    cur[i]+=1\n",
    "                elif arr[i]>arr[i-1] and arr[i]>arr[i+1]:\n",
    "                    cur[i]-=1\n",
    "            if cur==arr:\n",
    "                print(arr)\n",
    "                return arr\n",
    "            arr=cur[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "\n",
    "        res = [0 for _ in range(len(arr))]\n",
    "        res[0] = arr[0]\n",
    "        res[-1] = arr[-1]\n",
    "\n",
    "        d = {}\n",
    " \n",
    "        while True:\n",
    "\n",
    "            for i in range(1, len(arr) - 1):\n",
    "                if i in d:\n",
    "                    continue\n",
    "            \n",
    "                if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:\n",
    "                    res[i] = arr[i] + 1\n",
    "                elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:\n",
    "                    res[i] = arr[i] - 1\n",
    "                else:\n",
    "                    res[i] = arr[i]\n",
    "                    d[i] = 0\n",
    "            \n",
    "            cnt = 0\n",
    "            \n",
    "            for j in range(1, len(res) - 1):\n",
    "                if res[j] < res[j + 1] and res[j] < res[j - 1]:\n",
    "                    cnt += 1\n",
    "                elif res[j] > res[j + 1] and res[j] > res[j - 1]:\n",
    "                    cnt += 1\n",
    "            if cnt == 0:\n",
    "                break\n",
    "            \n",
    "    \n",
    "            arr = copy.deepcopy(res)\n",
    "\n",
    "   \n",
    "        return res\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def transformArray(self, arr: List[int]) -> List[int]:\n",
    "        n = len(arr)\n",
    "        while True:\n",
    "            tmp = arr.copy()\n",
    "            flag = True\n",
    "            for i in range(1, n-1):\n",
    "                if tmp[i-1] < tmp[i] > tmp[i+1]:\n",
    "                    arr[i] -= 1\n",
    "                    flag = False\n",
    "                if tmp[i-1] > tmp[i] < tmp[i+1]:\n",
    "                    arr[i] += 1\n",
    "                    flag = False\n",
    "            if flag: break\n",
    "        return arr\n",
    "        \n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
